﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoboRun
{
    class IfElse
    {
        public static int userEntry;
        private static int squaresCount;

        public static string comparativeOperator;
        //possible comparativeOperators: greater than, less than, exactly, greater than or equal to, less than or equal to

        public static bool ifElse()
        {
            squaresCount = checkNumberOfSquares(GameplayScreen.RobotCurrentPosition);
            GameplayScreen.debugger.Text = squaresCount.ToString();
            if (useAppropriateComparativeOperator() == true)
            {
                
                squaresCount = 0;
                return true;
            }
            else
            {
                squaresCount = 0;
                return false;
            }
        }

        private static int checkNumberOfSquares(String currentPosition)
        {
            char[] positionArray = currentPosition.ToCharArray();
            char x = positionArray[0];
            char y = positionArray[2];
            int xToInt = Int32.Parse(x.ToString());
            int yToInt = Int32.Parse(y.ToString());

            //variables below used in checking obstacles ahead
            String posToCheck;
            int newX;
            int newY;

            switch (GameplayScreen.RobotCurrentDirection)
            {
                case "North":
                    newX = xToInt - 1;
                    if (newX <= 0) //if coordinate becomes 0 then break!
                    {
                        return squaresCount;
                    }
                    posToCheck = (newX + "," + y);
                    for (int i = 0; i < GameplayScreen.ObstaclePositions.Length; i++)
                    {
                       
                        if (GameplayScreen.ObstaclePositions[i].Equals(posToCheck)) //if obstacle is ahead then return
                        {
                            return squaresCount;
                        }

                    }
                    squaresCount++; //otherwise increment counter
                    checkNumberOfSquares(posToCheck);
                    break;
                case "East":
                    newY = yToInt + 1;
                    if (GameplayScreen.GridType.Equals("3x3"))
                    {
                        if (newY > 3)
                        {
                            return squaresCount;
                        }
                    }
                    else if (GameplayScreen.GridType.Equals("4x4"))
                    {
                        if (newY > 4)
                        {
                            return squaresCount;
                        }
                    }
                    posToCheck = (x + "," + newY);
                    for (int i = 0; i < GameplayScreen.ObstaclePositions.Length; i++)
                    {
                        if (GameplayScreen.ObstaclePositions[i].Equals(posToCheck)) //if obstacle is ahead then return
                        {
                            return squaresCount;
                        }
                    }
                    squaresCount++;
                    checkNumberOfSquares(posToCheck);
                    break;
                case "South":
                    newX = xToInt + 1;
                    if (GameplayScreen.GridType.Equals("3x3"))
                    {
                        if (newX > 3)
                        {
                            return squaresCount;
                        } 
                    }
                    else if (GameplayScreen.GridType.Equals("4x4"))
                    {
                        if (newX > 4)
                        {
                            return squaresCount;
                        } 
                    }
                    posToCheck = (newX + "," + y);
                    for (int i = 0; i < GameplayScreen.ObstaclePositions.Length; i++)
                    {
                        if (GameplayScreen.ObstaclePositions[i].Equals(posToCheck)) //if obstacle is ahead then return
                        {
                            return squaresCount;
                        }
                    }
                    squaresCount++;
                    checkNumberOfSquares(posToCheck);
                    break;
                case "West":
                    newY = yToInt - 1;
                    if (newY <= 0)
                    {
                        return squaresCount;
                    }
                    posToCheck = (x + "," + newY);
                    for (int i = 0; i < GameplayScreen.ObstaclePositions.Length; i++)
                    {
                        if (GameplayScreen.ObstaclePositions[i].Equals(posToCheck))
                        {
                            return squaresCount;
                        }
                    }
                    squaresCount++;
                    checkNumberOfSquares(posToCheck);
                    break;
            }
            return squaresCount;
            }

        private static bool useAppropriateComparativeOperator()
        {
            switch (comparativeOperator)
            {
                case ("Greater than"):
                    if (squaresCount > userEntry)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ("Less than"):
                    if (squaresCount < userEntry)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ("Exactly"):
                    if (squaresCount == userEntry)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ("Greater than or equal to"):
                    if (squaresCount >= userEntry)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case ("Less than or equal to"):
                    if (squaresCount <= userEntry)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
            }
            return false;
        }

        }
    
}
